package com.dragee.modules.work.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragee.common.PageUtils;
import com.dragee.constants.CommonConstants;
import com.dragee.modules.sys.service.ISysRoleService;
import com.dragee.modules.sys.service.UserRoleService;
import com.dragee.modules.sys.service.UserService;
import com.dragee.modules.work.dao.TeamRefereeDao;
import com.dragee.modules.work.dto.TeamPerson;
import com.dragee.modules.work.entity.ApplyEntity;
import com.dragee.modules.work.entity.TeamRefereeEntity;
import com.dragee.modules.work.service.ApplyService;
import com.dragee.modules.work.service.TeamRefereeService;
import com.dragee.modules.work.service.TeamService;
import com.dragee.utils.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("teamRefereeService")
public class TeamRefereeServiceImpl extends ServiceImpl<TeamRefereeDao, TeamRefereeEntity> implements TeamRefereeService {


    @Resource
    private TeamRefereeDao teamRefereeDao;
    @Autowired
    private UserService userService;
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private TeamService teamService;
    @Autowired
    private ApplyService applyService;

    private String business = "teamReferee";
    /**
    * 列表
    */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        int limit = params.get(CommonConstants.LIMIT) == null
                ?CommonConstants.LIMIT_SIZE:Integer.parseInt(params.get(CommonConstants.LIMIT).toString());
        int start = params.get(CommonConstants.START) == null
                ?CommonConstants.START_SIZE:Integer.parseInt(params.get(CommonConstants.START).toString());
        QueryWrapper<TeamRefereeEntity> wrapper =
                WrapperUtil.getWrapper(business, params, TeamRefereeEntity.class);
        IPage<TeamRefereeEntity> page = new Page<>(start, limit);
        page = baseMapper.selectPage(page, wrapper.select());
        return new PageUtils(page);
    }

    @Override
    public List<TeamPerson> getApplyRefereeByApplyId(String applyId) {
        List<TeamPerson> result = new ArrayList<>();
        ApplyEntity apply = applyService.getById(applyId);
        String refereeIds = apply.getRefereeIds();
        if (StrUtil.isBlank(refereeIds)) {
            return result;
        }
        String[] split = refereeIds.split(",");
        for (int i = 0; i < split.length; i++) {
            TeamRefereeEntity refereeEntity = getById(split[i]);
            result.add(new TeamPerson(refereeEntity));
        }
        return result;
    }
}